<?php
class Zend_View_Helper_Truncate extends Zend_View_Helper_Abstract{
    private $_config;

    public function truncate($string, $length = 80, $etc = '...', $return_array = false,  $break_words = false, $middle = false,  $encoding = 'utf8')    {
        if ($length == 0) return '';
        // $string has utf-8 encoding
        if (mb_strlen($string, $encoding) > $length) {
            $length -= min($length, mb_strlen($etc, $encoding));
            if (!$break_words && !$middle) {
                $parsedString = preg_replace('/\s+?(\S+)?$/u', '', mb_substr($string, 0, $length + 1, $encoding));
                if($return_array){
                    $parsedStringExtended = str_ireplace($parsedString, '', $string);
                    return array('precut'=>$parsedString, 'postcut'=>$parsedStringExtended);
                }
                $string = $parsedString;
            }
            if (!$middle) {
                return mb_substr($string, 0, $length, $encoding) . $etc;
            } else {
                return mb_substr($string, 0, $length / 2, $encoding) . $etc . mb_substr($string, - $length / 2, $encoding);
            }
        } else {
            return $string;
        }
    }
}